package com.example.xjtuse_pc.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.xjtuse_pc.db.AppLockDBOpenHelper;
import com.example.xjtuse_pc.db.BlackNumberDBOpenHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by PC on 2016-5-20.
 */
public class AppLockDao {
    private AppLockDBOpenHelper helper;
    private Context context;

    public AppLockDao(Context context) {
        helper = new AppLockDBOpenHelper(context);
        context=this.context;
    }

    /**
     * 添加一个App包
     *
     * @param packname
     */
    public void add(String packname) {
        SQLiteDatabase sd = helper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("packname", packname);
        sd.insert("applock", null, cv);
        sd.close();
        Intent i=new Intent();
        i.setAction("com.example.xjtuse.lockchange");
        context.sendBroadcast(i);


    }

    /**
     * 删除一个App包
     *
     * @param packname
     */
    public void delet(String packname) {
        SQLiteDatabase sd = helper.getWritableDatabase();
        sd.delete("applock", "packname=?", new String[]{packname});
        sd.close();
        Intent i=new Intent();
        i.setAction("com.example.xjtuse.lockchange");
        context.sendBroadcast(i);

    }

    /*
     * 查询一个App包
     * @param packname
     */
    public boolean find(String packname) {
        boolean status = false;
        SQLiteDatabase sd = helper.getReadableDatabase();
        Cursor cursor = sd.query("applock", null, "packname=?", new String[]{packname}, null, null, null);
        if (cursor.moveToNext()) {
            status = true;
        }
        return status;
    }
    /*
    * 查询全部App包
    */
    public List<String> findAll() {
        List<String> ls=new ArrayList<String>();
        SQLiteDatabase sd = helper.getReadableDatabase();
        Cursor cursor = sd.query("applock", new String[]{"packname"},null,null, null, null, null);
        if (cursor.moveToNext()) {
            ls.add(cursor.getString(0));
        }
        return ls;
    }

}
